package com.socket;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client01 {
    public static void main(String[] args) throws IOException {
        //客户端完成发送图片给服务端,服务端接受图片并写入指定文件,并回复已收到

        //连接服务端
        Socket socket=new Socket(InetAddress.getLocalHost(),9999);
        //读取图片并将其转换为字节数组
        String path="D:\\javacode\\javacode\\J2024_11_21\\gold.png";
        BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(path));
        byte []bytes=SocketTools.streamtoByteArray(bufferedInputStream);
        //通过socket获取输出流,将字节数组发送给服务端
        BufferedOutputStream bos=new BufferedOutputStream(socket.getOutputStream());
        bos.write(bytes);
        socket.shutdownOutput();
        //读取服务端回复的消息
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//        System.out.println(bufferedReader.read());
        int readlen=0;
        char[]message=new char[1024];
        while((readlen=bufferedReader.read(message))!=-1){
            System.out.println(new String(message,0,readlen));
        }

        //关闭流
        bos.close();
        bufferedInputStream.close();
        bufferedReader.close();
        socket.close();


    }
}
